<?php
/**
 * Copyright 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *
 * @author Nadim Ratani <nratani@google.com>
 * @version 1.0
 */
include "jwt.php";
include "config.php";

$order = array(
  "drink" => $_POST["drink"],
  "size" => $_POST["size"],
  "milk" => $_POST["milk"],
  "pickup" => $_POST["pickup"],
  "total" => $_POST["formTotal"]
);

// Decode and verify the masked wallet response JWT
$masked_wallet_response = JWT::decode($_POST["maskedWalletJWT"], $Config["signing_key"]);
$pay = $masked_wallet_response->selection->pay;
$ship = $masked_wallet_response->selection->ship;

// Create a masked wallet request JWT for the Google Wallet Edit Button
$masked_wallet_request = array(
  "aud" => "Google",
  "iat" => (int)date("U"),
  "iss" => $Config["iss"],
  "origin" => $Config["origin"],
  "google_transaction_id" => $masked_wallet_response->google_transaction_id,
  "selectors" => array("pay" => array("type" => "PAYMENT_INSTRUMENT"),
                       "ship" => array("type" => "SHIPPING_ADDRESS")),
  "typ" => "google/paymentexpress/v1/masked/request");
$masked_wallet_request_jwt = JWT::encode($masked_wallet_request, $Config["signing_key"]);

// Create a full wallet request JWT
$full_wallet_request = array(
  "iss" => $Config["iss"],
  "aud" => "Google",
  "typ" => "google/paymentexpress/v1/full/request",
  "iat" => (int)date("U"),
  "google_transaction_id" => $masked_wallet_response->google_transaction_id,
  "origin" => $Config["origin"],
  "cart" => array(
    "total_price" => $order["total"],
    "currency" => $Config["currency"],
    "line_items" => array(array(
      "description" => $order["drink"],
      "quantity" => 1,
      "unit_price" => $order["total"],
      "total_price" => $order["total"],
      "currency" => $Config["currency"]
    ))
  )
);
$full_wallet_request_jwt = JWT::encode($full_wallet_request, $Config["signing_key"]);
?>
<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>

<title>Wallet Baristas</title>

<link rel="stylesheet" type="text/css" href="css/style.css" media="screen" />

  <script type="text/javascript"
        language="javascript"
          src="<?php echo $Config["jsURL"] ?>">
  </script>

<script type="text/javascript"
        language="javascript"
        src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js">
</script>

<script type='text/javascript'>

// Handle wallet failure case
function failureFullWallet(error, detail) {
    log('--------------------------');
    log("There was an error getting the full wallet: " + error + "\n" + detail);
}

// Logging for demonstration purposes
function log(msg) {
  if (console && console.log) {
    console.log(msg);
  }
}

//////////////////////////////////////////////////////////////////////
// Wallet control
//////////////////////////////////////////////////////////////////////

function successMaskedWallet(jwt) {
  log('--------------------------');
  log("Handling the masked wallet.");
  log(jwt);

  // populate the hidden form field with the masked wallet contents
  $("#maskedWalletJWT").val(jwt);

  // submit the form
  document.forms["updateCredentials"].submit();
}

function failureMaskedWallet(error, detail) {
    log('--------------------------');
    log("There was an error getting the masked wallet: " + error + "\n" + detail);
}

</script>

</head>
<body>
  <div id="header">
    <h1>Wallet Baristas</h1>
  </div>
  
  <form id="purchaseConfirmForm" name="purchaseConfirmForm" 
        action="receipt.php"
        method="post"
        onSubmit="return goog.wallet.requestFullWallet(&quot;<?php echo $full_wallet_request_jwt ?>&quot;, &quot;fullWalletJWT&quot;, failureFullWallet);">

  <div id="finish_order">
  	<h2>Order Details</h2>
    <div id="formatBar">
      <table>
      	<tr>
      	  <td>
          <div class="offset"><strong>Order: </strong><?php echo $order["drink"] ?></div>
          <div class="offset"><strong>Total: $</strong><?php echo $order["total"] ?></div>
      	  </td>
      	</tr>
      </table>
      


      <!-- orderDescription and fullWalletRequestJWT below are only used to communicate state back to the server.
           A real implementation will not need to use this technique and will only need the fullWalletJWT input. -->
           <input type="hidden" name="orderDescription" id="orderDescription" value="<?php echo $order["drink"] ?>"> 
           <input type="hidden" name="formTotal" id="formTotal" value="<?php echo $order["total"] ?>"> 
           <input type="hidden" name="fullWalletRequestJWT" id="fullWalletRequestJWT" value="<?php echo $full_wallet_request_jwt ?>"> 
      <input type="hidden" name="fullWalletJWT" id="fullWalletJWT"> 
      <div class="offset"><input type="submit" value="Finish & Pay"></div>
    </div>
    
    <h2>Payment Details</h2>
    <h4>Card Details</h4>
    <div id="formatBar">
    <div class="offset"><b>name: </b><?php echo $pay->payment_instrument->billing_address->name ?></div>
    <div class="offset"><b>card name: </b><?php echo $pay->payment_instrument->descriptive_name ?></div>
    <div class="offset"><b>type: </b><?php echo $pay->payment_instrument->type ?></div>
    <div class="offset"><b>last 4 digits: </b><?php echo $pay->payment_instrument->last_four_digits ?></div>
    <div class="offset"><b>expiration: </b><?php echo $pay->payment_instrument->expiration_month ?>/<?php echo $pay->payment_instrument->expiration_year ?></div>
    </div>
    <h4>Billing Address</h4>
    <div id="formatBar">
    <div class="offset"><b>company: </b><?php echo $pay->payment_instrument->billing_address->company_name ?></div>
    <div class="offset"><b>address1: </b><?php echo $pay->payment_instrument->billing_address->address1 ?></div>
    <div class="offset"><b>address2: </b><?php echo $pay->payment_instrument->billing_address->address2 ?></div>
    <div class="offset"><b>city: </b><?php echo $pay->payment_instrument->billing_address->city ?></div>
    <div class="offset"><b>state: </b><?php echo $pay->payment_instrument->billing_address->state ?></div>
    <div class="offset"><b>postal code: </b><?php echo $pay->payment_instrument->billing_address->postal_code ?></div>
    <div class="offset"><b>country: </b><?php echo $pay->payment_instrument->billing_address->country_code ?></div>
    <div class="offset"><b>phone #: </b><?php echo $pay->payment_instrument->billing_address->phone_number ?></div>
    </div>
    <h4>Shipping Address</h4>
    <div id="formatBar">
    <div class="offset"><b>company: </b><?php echo $ship->shipping_address->company_name ?></div>
    <div class="offset"><b>address1: </b><?php echo $ship->shipping_address->address1 ?></div>
    <div class="offset"><b>address2: </b><?php echo $ship->shipping_address->address2 ?></div>
    <div class="offset"><b>city: </b><?php echo $ship->shipping_address->city ?></div>
    <div class="offset"><b>state: </b><?php echo $ship->shipping_address->state ?></div>
    <div class="offset"><b>postal code: </b><?php echo $ship->shipping_address->postal_code ?></div>
    <div class="offset"><b>country: </b><?php echo $ship->shipping_address->country_code ?></div>
    <div class="offset"><b>phone #: </b><?php echo $ship->shipping_address->phone_number ?></div>
      <br />
      <div class="offset"><g:wallet jwt="<?php echo $masked_wallet_request_jwt ?>" mode="change" onSuccess="successMaskedWallet(jwt)" 
      onFailure="failureMaskedWallet(error, detail)"></g:wallet></div>

  </div>
  </form>
  
  <form id="updateCredentials" name="updateCredentials" action="confirm.php" method="post">
  <input type="hidden" name="drink" id="drink" value="<?php echo $order["drink"] ?>" />
  <input type="hidden" name="size" id"size" value="<?php echo $order["size"] ?>" />
  <input type="hidden" name="milk" id"milk" value="<?php echo $order["milk"] ?>" />
  <input type="hidden" name="pickup" id="pickup" value="<?php echo $order["pickup"] ?>" />
  <input type="hidden" name="formTotal" id="formTotal" value="<?php echo $order["total"] ?>" />
    <input type="hidden" name="maskedWalletJWT" id="maskedWalletJWT" value="">
  </form>
</body>
</html>
